//
//  ShenPiXiangQingFuJianTableViewCell.m
//  JuBoCRM
//
//  Created by iMac on 2024/3/14.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "ShenPiXiangQingFuJianTableViewCell.h"
#import "XieGenJinFuJianListTableViewCell.h"
@implementation ShenPiXiangQingFuJianTableViewCell{
    NSArray *dataSource;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
 
    [_mainTableView registerNib:[UINib nibWithNibName:@"XieGenJinFuJianListTableViewCell" bundle:nil] forCellReuseIdentifier:@"XieGenJinFuJianListTableViewCell"];
    
}

- (void)setModel:(AllModel *)model {
    _model = model;
    NSDictionary *dataDic = model.modelInfoDictionary;
    
    if([[dataDic objectForKey:@"file"] isKindOfClass:[NSArray class]]){
        dataSource = [dataDic objectForKey:@"file"];
    }
    
    _tableViewHeight.constant = dataSource.count*52;
    
    [_mainTableView reloadData];
    
    [self setupAutoHeightWithBottomView:_mainTableView bottomMargin:9];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XieGenJinFuJianListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XieGenJinFuJianListTableViewCell"];
    NSDictionary *dataDic = dataSource[indexPath.row];
    cell.nameLabel.text = GetHandleString(dataDic, @"name");
    cell.sizeLabel.text = [self fileSizeConversion:[GetHandleString(dataDic, @"size") integerValue]];
    cell.manageClickBlock = ^(NSInteger index) {
        if(_manageClickBlock){
            _manageClickBlock(dataDic,index);
        }
    };
    return cell;
}


//将文件大小转换为 M/KB/B
- (NSString *)fileSizeConversion:(NSInteger)totalSize {
    NSString *totleStr = nil;
    if (totalSize > 1024 * 1024) {
        totleStr = [NSString stringWithFormat:@"%.2fM",totalSize / 1024.00f /1024.00f];
    } else if (totalSize > 1024) {
        totleStr = [NSString stringWithFormat:@"%.2fKB",totalSize / 1024.00f ];
        
    } else {
        totleStr = [NSString stringWithFormat:@"%.2fB",totalSize / 1.00f];
    }
    return totleStr;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 52;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
